package com.mp.approvalreq.util;

import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.ContentType;
import com.plusmoney.exception.RpcServerLogicalException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by liuqi on 2018/9/19.
 */
class ContentTypeCheck {
	private static final Logger logger = LoggerFactory.getLogger(ContentTypeCheck.class);
	
	static void checkContent(int componentType, String name, String value, int need, List<String> chosens) {
		logger.info("type:{}, name:{}, value:{}, need:{}, chosen:{}", componentType, name, value, need, chosens);
		
		switch (componentType) {
			case ContentType.MONEY:
				checkMoney(name, value, need == 1);
				break;
			
			case ContentType.GROUP_SINGLE_CHOSEN:
				checkGroupSingleChosen(name, value, chosens, need == 1);
				break;
				
			case ContentType.GROUP_LOCATION:
				checkGroupLocation(name, value, need == 1);
				break;
				
			default:
				commonCheck(name, value, need == 1);
				break;
		}
	}
	
	static class Location {
		String location;
		String longitude;
		String latitude;
		
		public String getLocation() {
			return location;
		}
		
		public void setLocation(String location) {
			this.location = location;
		}
		
		public String getLongitude() {
			return longitude;
		}
		
		public void setLongitude(String longitude) {
			this.longitude = longitude;
		}
		
		public String getLatitude() {
			return latitude;
		}
		
		public void setLatitude(String latitude) {
			this.latitude = latitude;
		}
	}
	
	private static void checkGroupLocation(String name, String value, boolean require) {
		if (require && StringUtils.isEmpty(value)) {
			throw new RpcServerLogicalException(1, name + "不能为空");
		}
		
		if (require) {
			Location location = JSONObject.parseObject(value, Location.class);
			if (StringUtils.isEmpty(location.getLocation())) {
				throw new RpcServerLogicalException(1, "地点不能为空");
			}
			if (StringUtils.isEmpty(location.getLatitude())) {
				throw new RpcServerLogicalException(1, "纬度不能为空,请重新定位");
			}
			if (StringUtils.isEmpty(location.getLongitude())) {
				throw new RpcServerLogicalException(1, "经度不能为空,请重新定位");
			}
		}
	}
	
	private static void commonCheck(String name, String value, boolean require) {
		if (require && StringUtils.isEmpty(value)) {
			throw new RpcServerLogicalException(1, name + "不能为空");
		}
	}
	
	private static void checkMoney(String name, String value, boolean require) {
		if (require && StringUtils.isEmpty(value)) {
			throw new RpcServerLogicalException(1, name + "不能为空");
		}
		
		if (StringUtils.isNotEmpty(value)) {
			if (!ValidateUtil.isMoney(value)) {
				throw new RpcServerLogicalException(1, name + "仅支持输入数字");
			}
		}
	}
	
	
	private static void checkGroupSingleChosen(String name, String value, List<String> chosens, boolean require) {
		if (require && StringUtils.isEmpty(value)) {
			throw new RpcServerLogicalException(1, name + "不能为空");
		}
		
		if (ListHelper.isNullEmpty(chosens)) {
			throw new RpcServerLogicalException(1, name + "没有选项");
		}
		
		if (StringUtils.isNotEmpty(value)) {
			if (!chosens.contains(value)) {
				throw new RpcServerLogicalException(1, value + "选项不存在");
			}
		}
	}
}

